Examining the Workflow 4.0 Activities

Recall that the purpose of WF is to allow you to model a business process in a declarative manner, which is then executed by the WF runtime engine. In the vernacular of WF, a business process is composed of any number of activities. Simply put, a WF activity is an atomic “step” in the overall process. When you create a new workflow application, you will find the Toolbox contains iconic representations of the builtin activities grouped by category.

These out-of-the-box activities are used to model your business process. Each activity in the Toolbox maps to a real class within the System.Activities.dll assembly (most often contained within the System.Activities.Statements namespace). You’ll make use of several of these baked-in activities over the course of this chapter; however, here is a walkthrough of many of these default activities. As always, consult the .NET Framework 4.0 SDK documentation for full details.

Control Flow Activities

The first category of activities in the toolbox allow you to represent looping and decision tasks in a larger workflow. Their usefulness should be easy to understand, given that we do similar tasks in C# code quite often. In Table 26-1, notice that some of these control flow activities allow for parallel processing of activities using the Task Parallel Library behind the scenes (see Chapter 19).

Table 26-1. The control flow activities of WF 4.0

Activities Meaning in Life
DoWhile A looping activity that executes contained activities at least once, until a condition is no longer true.
ForEach<T> Executes an activity action once for each value provided in the ForEach<T>.Values collection.
If Models an If-Then-Else condition.
Parallel An activity that executes all child activities simultaneously and asynchronously.
ParallelForEach<T> Enumerates the elements of a collection and executes each element of the collection in parallel.
Pick Provides event-based control flow modeling.
PickBranch A potential path of execution within a parent Pick activity.
Sequence Executes a set of child activities sequentially.
Switch<T> Selects one choice from a number of activities to execute, based on the value of a given expression of the type specified in this object’s type parameter.
While Executes a contained workflow element while a condition evaluates to true.

Flowchart Activities

Next are the flowchart activities, which are actually quite important given that the Flowchart activity will very often be the first item you place on your WF designer. The concept of a flow chart workflow is new with .NET 4.0. It allows you to build a workflow using the well known flow chart model, where the execution of the workflow is based on numerous branching paths, each of which is based on the truth or falsity of some internal condition. Table 26-2 documents each member of this activity set.

Table 26-2. The flowchart activities of WF 4.0

Activities

Meaning in Life

Flowchart

Models workflows using the familiar flowchart paradigm. This is often the very first activity you will place on a new designer.

FlowDecision

A node that provides the ability to model a conditional node with two possible outcomes.

FlowSwitch<T>

A node that allows modeling a switch construct, with one expression and one outcome for each match.

Messaging Activities

A workflow can easily invoke members of an external XML web service or WCF service, as well as be notified by an external service using messaging activities. Because these activities are very closely related to WCF development, they have been packaged up in a dedicated .NET assembly, System.ServiceModel.Activities.dll. Within this library, you will find an identically named namespace defining the core activities seen in Table 26-3.

Table 26-3. The messaging activities of WF 4.0

Activities Meaning in Life
CorrelationScope Used to manage child message activities.
InitializeCorrelation Initializes correlation without sending or receiving a message.
Receive Receives a message from a WCF service.
Send Sends a message to a WCF service.
SendAndReceiveReply Sends a message to a WCF service and captures the return value.
TransactedReceiveScope An activity that enables you to flow a transaction into a workflow or dispatcher-created server side transactions.

The most common messaging activities are Send and Receive, which allow you to communicate with external XML web services or WCF services.

The Runtime and Primitives Activities

The next two categories in the toolbox, Runtime and Primitives, allow you to build a workflow which makes calls to the workflow runtime (in the case of Persist and TerminateWorkflow) and performs common operations such as push text to an output stream or invoke a method on a .NET object. Consider Table 26-4.

Table 26-4. The runtime and primitive activities of WF 4.0

Activities Meaning in Life
Persist Requests that a workflow instance persist its state into a database using the WF persistence service.
TerminateWorkflow Terminates the running workflow instance, raises the WorkflowApplication.Completed event in the host, and reports error information. Once the workflow is terminated, it cannot be resumed.
Assign Allows you to set properties on an activity using the assignment values you defined via the workflow designer.
Delay Forces a workflow to stop for a fixed amount of time.
InvokeMethod Calls a method of a specified object or type.
WriteLine Writes a specified string to a specified TextWriter derived type. By default, this will be the standard output stream (a.k.a. the console); however, you can configure other streams , such as a FileStream.

InvokeMethod is maybe the most interesting and useful activity of this set because it allows you to call methods of .NET classes in a declarative manner. You can also configure InvokeMethod to hold onto any return value send from the method you call. TerminateWorkflow can also be helpful when you need to account for a point of no return. If the workflow instance hits this activity, it will raise the Competed event which can be caught in the host, just like you did in the first example.

The Transaction Activities

When you are building a workflow, you might need to ensure that a group of activities work in an atomic manner, meaning they must all succeed or all fail as a collective group. Even if the activities in question are not directly working with a relational database, the core activities seen in Table 26-5 allow you to add a transactional scope into a workflow.

Table 26-5. The transaction activities of WF 4.0

Activities Meaning in Life
CancellationScope Associates cancellation logic within a main path of execution.
CompensableActivity An activity that supports compensation of its child activities.
TransactionScope An activity that demarcates a transaction boundary.

The Collection and Error Handling Activities

The final two categories to consider in this introductory chapter allow you to declaratively manipulate generic collections and respond to runtime exceptions. The collection activities are great when you need to manipulate objects which represent business data (such as purchase orders, medical information objects, or order tracking) on the fly in XAML. Error activities, on the other hand, allow you to essentially author try/catch/throw logic within a workflow. Table 26-6 documents this final set of WF 4.0 activities.

Table 26-6. The collection and error handling activities of WF 4.0

Activities Meaning in Life
AddToCollection<T> Adds an item to a specified collection.
ClearCollection<T> Clears a specified collection of all items.
ExistsInCollection<T> Indicates whether a given item is present in a given collection.
RemoveFromCollection<T> Removes an item from a specified collection.
Rethrow Throws a previously thrown exception from within a Catch activity.
Throw Throws an exception.
TryCatch Contains workflow elements to be executed by the workflow runtime within an exception handling block.

OK! Now that we have seen many of the default activities at a high level, we can start to build some more interesting workflows that make use of them. Along the way, you will learn about the two key activities that typically function as the root of your workflow, Flowchart and Sequence.